package gxprod.androidai.functions;

import android.util.Log;
import gxprod.androidai.AIActivity;
import gxprod.androidai.BubbleAdapter;
import gxprod.androidai.DataCollection;
import gxprod.androidai.regex.MatchResult;
import gxprod.androidai.regex.NamedPattern;


/**
 * REmembers information about and for the user. 
 * @author Josh Maleszewski
 */
public class MemoryFunction extends Function {

	private NamedPattern[] patterns = new NamedPattern[] {
			NamedPattern.compile("remember that ([\\w ]+) (is|are|was) ([\\w ]+)"),
			NamedPattern.compile("remember ([\\w ]+) (is|are|was) ([\\w ]+)"),
			NamedPattern.compile("recall ([\\w ]+)")
			// What do you remember - seperate activity
	};

	@Override
	public MatchResult isMatch(String statement) {
		return super.isMatch(statement, patterns, 0.55f);
	}

	@Override
	public boolean execute(MatchResult result, BubbleAdapter adapter) {

		String tag, value;
		
		switch(result.MatchIndex) {

			case 0:
			case 1:
				tag = result.Matcher.group(1);
				value = result.Matcher.group(3);
				
				DataCollection.Memory.put(tag, value);
				
				adapter.output("I will remember");
				break;

			case 2:
				tag = result.Matcher.group(1);
				String answer;

				answer = (String) DataCollection.Memory.get(tag);
				
				// TODO: search items to find close matches
				
				if(answer != null)
					adapter.output("I dont recall...");
				break;

			default :
				Log.e(AIActivity.TAG, "invalid match result index: " + result.MatchIndex + " in InteractionFunction");
				return false;
		}
		return true;
	}

	@Override
	public boolean cullFunction(String statement) { return true; }
}
